package com.zhaodui.util.word;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class WordTemplateUtilities {
    private static final WordTemplateUtilities word = new WordTemplateUtilities();

    private static WordTemplate template;

    private WordTemplateUtilities() {
    }

    public static WordTemplateUtilities GetInstance() {
        return word;
    }

    public void toWord(String infilePath, String outfilePath, Map<String, Object> map) {
        File intfile = new File(infilePath);
        FileInputStream fileInputStream = null;
        File outfile = new File(outfilePath);
        FileOutputStream out;
        try {
            fileInputStream = new FileInputStream(intfile);
            template = new WordTemplate(fileInputStream);

            template.replaceTag(map);// 鏇挎崲瀛楃涓叉ā鐗�

            out = new FileOutputStream(outfile);
            BufferedOutputStream bos = new BufferedOutputStream(out);
            template.write(bos);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
    
	public static void convertWordToPdf(String wordPath, String pdfPath) throws IOException {

		FileInputStream inpuFile = new FileInputStream(wordPath);
		File outFile=new File(pdfPath);
		
	    //从输入的文件流创建对象
	    XWPFDocument document = new XWPFDocument(inpuFile);

	    //创建PDF选项
	    PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding("windows-1250")

	    //为输出文件创建目录
	    outFile.getParentFile().mkdirs();

	    //执行PDF转化
	    PdfConverter.getInstance().convert(document, new FileOutputStream(outFile), pdfOptions);
	    
	    System.out.println("成功转换:"+pdfPath);

	}
}
